// 引入路由模块
const { Router } = require('express')
// 创建一个路由实例
const router = new Router()

// 引入用户信息表模型
const userImformationModel = require('../../model/userImformation')

router.post('/changePassword',(request, response)=>{
    const { old, ofNew, user, _id } = request.body
    let data = {
        code: '200'
    }
    if( old == ofNew ){
        data.code = '301'
        data.msg = data.data = '修改密码失败，新密码与原密码一致'
        return response.send(JSON.stringify(data))
    }
    userImformationModel.findOneAndUpdate({
        count: user,
        _id,
        password: old
    },{
        password: ofNew
    },function (err,doc) {
        if (err){
            data.msg = '修改密码失败'
        }else {
            if (!doc && typeof doc != "undefined" && doc != 0){
                data.msg = '密码错误'
                data.data = 'error'
            }else {
                data.msg = '修改成功'
                data.data = 'success'
            }
        }
        response.send(JSON.stringify(data))
    })
})

module.exports = router
